<?php 
	class Db_member extends CI_Model{
		public function __construct(){
			parent::__construct();
		}
		
		public function datalist(){
			/* ตัวแปร แบบ array เก็บฟิวด์ใน ฐานข้อมูล ต้องตรงกัน กับ จำนวน columns ที่อยู่ในไฟล์ server_side.php ด้วย
			 */
			$aColumns = array( 't1.pkid','t1.name','t3.card_no','t1.customer_pkid','t1.address','t1.pkid');
			/* กำหนด primary key ให้กับคอลัมน์ */
			$sIndexColumn = "t1.pkid";
			/* ชื่อตาราง */
			$sTable = "mobile_member t1  JOIN mobile_customer t2  ON t1.customer_pkid = t2.pkid
						    JOIN mobile_card t3 ON t1.card_pkid=t3.pkid ";
			/*
			 * แบ่งหน้า
			*/
			$sLimit = "";
			if ($this->input->post('iDisplayStart') != ""  && ($this->input->post('iDisplayLength') != '-1' ))
			{
				$sLimit = "LIMIT ".$this->input->post('iDisplayStart') .", ".
						$this->input->post('iDisplayLength') ;
			}
			/*
			 * จัดเรียงลำดับ
			*/
			$sOrder = "";
			if ($this->input->post('iSortCol_0') != "" )
			{
				$sOrder = "ORDER BY  ";
				for ( $i=0 ; $i<intval( $this->input->post('iSortingCols')) ; $i++ )
				{
					if ($this->input->post('bSortable_'.intval($this->input->post('iSortCol_'.$i)) ) == "true" )
					{
						$sOrder .= $aColumns[ intval( $this->input->post('iSortCol_'.$i) ) ]."
						".$this->input->post('sSortDir_'.$i) .", ";
					}
				}
					
				$sOrder = substr_replace( $sOrder, "", -2 );
				if ( $sOrder == "ORDER BY" )
				{
					$sOrder = "";
				}
			}
			
			$sWhere = "";
			if($this->session->userdata("sess_customer_name") != "eshine"){
				//$this->db->where("t1.customer_pkid",$this->session->userdata("sess_customer_id"));
				$customerpkid = $this->session->userdata("sess_customer_id");
				$sWhere .= " AND t1.customer_pkid='$customerpkid' ";
			}
			if ($this->input->post('sSearch') != "" && $this->input->post('sSearch') != "" )
			{
				
				$sWhere = "WHERE (";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$sWhere .= $aColumns[$i]." LIKE '%". $this->input->post('sSearch') ."%' OR ";
				}
				$sWhere = substr_replace( $sWhere, "", -3 );
				$sWhere .= ')';
			}
			
			/* เวลาค้นหาในคอลัมน์  */
			for ( $i=0 ; $i<count($aColumns) ; $i++ )
			{
				if ( $this->input->post('bSearchable_'.$i) != "" && $this->input->post('bSearchable_'.$i) == "true" && $this->input->post('sSearch_'.$i) != '' )
				{
					if ( $sWhere == "" )
					{
						$sWhere = "WHERE ";
					}
					else
					{
						$sWhere .= " AND ";
					}
					$sWhere .= $aColumns[$i]." LIKE '%".$this->input->post('sSearch_'.$i)."%' ";
				}
			}
			/*
			 *คำสั่ง SQL ดึงข้อมูลออกมาโชว์
			*
			*/
			$sQuery = "
			SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
			FROM   $sTable
			$sWhere
			$sOrder
			$sLimit
			";
			$rResult = $this->db->query( $sQuery);
			
			/* จำนวนข้อมูลหลังจากค้นหา */
			 $sQuery = "
			SELECT FOUND_ROWS() as numT
			";
			$rResultFilterTotal = $this->db->query($sQuery) ;
			$aResultFilterTotal = $rResultFilterTotal->row();
			$iFilteredTotal = $aResultFilterTotal->numT;
			
			/*  จำนวนทั้งหมด */
			//$sWhere .= " AND t1.customer_pkid='$customerpkid' ";
			$sQuery = "
			SELECT COUNT(".$sIndexColumn.") as num
			FROM   $sTable";
			if($this->session->userdata("sess_customer_name") != "eshine"){
				$customerpkid = $this->session->userdata("sess_customer_id");
				$sQuery .="WHERE t1.customer_pkid='$customerpkid'
			";
			}else{
				//$sQuery .="WHERE";
			}
			$rResultTotal = $this->db->query( $sQuery);
			$aResultTotal = $rResultTotal->row();
			$iTotal = $aResultTotal->num;
			
			
			/*
			* ส่วนการแสดงผล
			*/
			$output = array(
					"sEcho" => intval($this->input->post('sEcho')),
			"iTotalRecords" => $iTotal,
			"iTotalDisplayRecords" => $iFilteredTotal,
			"aaData" => array()
			);
			
			
			
			foreach ($rResult->result_array()  as $aRow)  //loop ออกมาแสดงข้อมูล
			{
					$row = array();
					for ( $i=0 ; $i<count($aColumns) ; $i++ )
					{
					if ( $aColumns[$i] == "version" )
					{
			$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
				
			}
			else if ( $aColumns[$i] != ' ' )
			{
			if($i == 0){
			$row[] = '<input type="checkbox" name="check[]" value="'.$aRow['pkid'].'" />';
			}else if($i == 1){
				$row[] = $aRow['card_no'];
			}else if($i == 2){
				$row[] = $aRow['name'];
			}else if($i == 3){
				///$row[] = $aRow['customer_pkid'];
				 $this->db->where("pkid",$aRow['customer_pkid']);
				 $rows = $this->db->get("mobile_customer")->row();
				 $row[] = $rows->name;
			}
			 else if($i == 4){
				$row[] = '
				 <a href="'.base_url().'apanel/member_detail/'.$aRow['pkid'].'" >
				 <img border="0" src="'.base_url().'images/zoom.png">
				 </a>';
				}else if($i == 5){
					if($this->session->userdata('sess_access')== 'superadmin'){
				$row[] = '
				<a href="'.base_url().'manage_member/member_del/'.$aRow['pkid'].'" onClick="return confirm(\'Are you sure delete item\');">
				<img border="0" src="'.base_url().'images/delete-icon.png">
				</a>';
				}else{
				$row[] = '
				<a href="'.base_url().'apanel/dashboard" onClick="alert(\'you do not have sufficient privileges to delete\');">
				<img border="0" src="'.base_url().'images/delete-icon.png">
				</a>';
				}
				}
				
				
				
				
					
			}
				}
				$output['aaData'][] = $row;
			}
			return $output;
		}
	}
?>